import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
const state = {
  count: 0,
};
const getters = {
  evenOrOdd(state) {
    return state.count % 2 === 1 ? "奇数" : "偶数";
  },
};
const actions = {
  evenAdd({ commit, state }, count) {
    if (state.count % 2 === 1) {
      commit("INCREMENT", count);
    }
  },
  asyncAdd({ commit }, count) {
    setTimeout(() => {
      commit("INCREMENT", count);
    }, 1000);
  },
};
const mutations = {
  INCREMENT(state, count) {
    state.count += count;
  },
  DECREMENT(state, count) {
    state.count -= count;
  },
};

export default new Vuex.Store({
  state,
  getters,
  actions,
  mutations,
});
